/***************************************************************************
 * The Open Armory API is a project to retrieve World of Warcraft(R) 
 * related XML data from The Armory.
 * 
 * The data retrieved via the Open Armory API from The Armory is 
 * proprietary to Blizzard Entertainment, Inc(R).
 * *************************************************************************
 * Copyright (C) 2009 - 2010 Tedla M.B. Brandsema
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/
package net.openarmory.core.domain;

import java.io.InputStream;

import net.openarmory.core.domain.constants.ArmoryContentGroup;
import net.openarmory.core.domain.constants.ArmoryZone;
import net.openarmory.core.domain.url.ArmoryItemUrl;
import net.openarmory.core.domain.url.ArmoryItemUrlBuilder;
import net.openarmory.core.domain.url.ArmoryUrl;
import net.openarmory.core.domain.url.ArmoryUrlFactory;
import net.openarmory.core.domain.url.UrlConnectionReader;

/**
 * The Class {@code ArmoryItemXmlRetriever} retrieves item related XML from The
 * Armory.
 */
public final class ArmoryItemXmlRetriever extends UrlConnectionReader {

	/** The item URL builder. */
	private final ArmoryItemUrlBuilder itemUrlBuilder;

	/** The armory URL. */
	private ArmoryUrl armoryUrl;

	/**
	 * This method returns the {@link ArmoryUrl} that is instantiated by either
	 * the
	 * {@link #fetchCharacterSpecificItemTooltipXml(ArmoryZone, int, String, String, int)}
	 * or {@link #fetchItemTooltipXml(ArmoryZone, int)} methods. The method
	 * returns {@code null} if
	 * {@link #fetchCharacterSpecificItemTooltipXml(ArmoryZone, int, String, String, int)}
	 * or {@link #fetchItemTooltipXml(ArmoryZone, int)}has not been called
	 * first.
	 * 
	 * @return The item specific armory URL.
	 */
	public ArmoryUrl getArmoryUrl() {
		return this.armoryUrl;
	}

	/**
	 * Instantiates a new armory item XML retriever.
	 */
	public ArmoryItemXmlRetriever() {
		this.itemUrlBuilder = (ArmoryItemUrlBuilder) ArmoryUrlFactory
				.getUrlBuilder(ArmoryContentGroup.ITEM);
	}

	/**
	 * Method for fetching an item from The Armory that is currently equipped by
	 * a character.
	 * 
	 * @param zone
	 *            The zone represents The Armory region (i.e. EU or US).
	 * @param itemId
	 *            The id number of the item.
	 * @param realm
	 *            The realm on which the character resides on.
	 * @param characterName
	 *            The character name.
	 * @param slot
	 *            The item slot where the item is equipped represented by an
	 *            integer value (i.e. head = 0, etc...).
	 * 
	 * @return The {@code InputStream} containing XML data from the requested
	 *         URL.
	 */
	public InputStream fetchCharacterSpecificItemTooltipXml(
			final ArmoryZone zone, final int itemId, final String realm,
			final String characterName, final int slot) {
		final ArmoryItemUrl itemUrl = this.itemUrlBuilder.getCharacterItemUrl(
				itemId, zone, realm, characterName, slot);
		this.armoryUrl = itemUrl;
		return fetchContent(itemUrl.getUrl());
	}

	/**
	 * Method for fetching an item from The Armory.
	 * 
	 * @param zone
	 *            The zone represents The Armory region (i.e. EU or US).
	 * @param itemId
	 *            The item slot where the item is equipped represented by an
	 *            integer value (i.e. head = 0, etc...).
	 * 
	 * @return The {@code InputStream} containing XML data from the requested
	 *         URL.
	 */
	public InputStream fetchItemTooltipXml(final ArmoryZone zone,
			final int itemId) {
		final ArmoryItemUrl itemUrl = this.itemUrlBuilder.getItemUrl(itemId,
				zone);
		this.armoryUrl = itemUrl;
		return fetchContent(itemUrl.getUrl());
	}

}
